package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionServlet extends HttpServlet {

	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("service()");  // 用于跟踪service方法是否被执行了，调试时用
		req.setCharacterEncoding("utf-8");// 设置服务器解码方式；值针对post提交方式有效；get请求的解决办法需要在配置文件中解决
		resp.setContentType("text/html;charset=utf-8"); // 设置返回给浏览器的内容形式和数据格式
		
		
//		分析资源请求路径
		String path = req.getServletPath();
		System.out.println(path);
		
//		请求分发及处理
		if("/check_name.do".equals(path)) {
			check_name(req, resp); // 处理get方式的请求
		}else if("/check_name2.do".equals(path)) {
			check_name(req, resp); // 处理post方式的请求
		}else if("/get_city.do".equals(path)) {
			get_city(req, resp);
		}else if("/random_number.do".equals(path)) {
			random_number(req, resp);
		}
//		System.out.println(req.getContextPath()); // 获取项目及明
//		System.out.println(req.getServletPath()); // 获取网名
//		System.out.println(req.getRequestURI()); // 获取请求资源绝对路径
//		System.out.println(req.getRequestURL()); // 获取请求资源完整路径
	}

	private void check_name(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
		System.out.println("check_name"); // 追踪
		PrintWriter out = resp.getWriter();
		
		
//		获取请求参数
		String username = req.getParameter("name"); // 这里的name是和请求参数名一致，不是form表单中的名字
		System.out.println(username); // 打印输出
		if("fury".equals(username)) {
			out.println("该用户名已经被占用，请重新输入");
//			System.out.println("占用");
		}else {
			out.println("该用户名可以使用");
//			System.out.println("未被占用");
		}
		
	}
	
	private void check_name2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
		System.out.println("check_name2"); // 追踪
		PrintWriter out = resp.getWriter();
		
		
//		获取请求参数
		String username = req.getParameter("name"); // 这里的name是和请求参数名一致，不是form表单中的名字
		System.out.println(username); // 打印输出
		if("fury".equals(username)) {
			out.println("该用户名已经被占用，请重新输入");
//			System.out.println("占用");
		}else {
			out.println("该用户名可以使用");
//			System.out.println("未被占用");
		}
		
	}
	
	private void get_city(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
		System.out.println("get_city()"); // 追踪
		PrintWriter out = resp.getWriter();
		
		String provice = req.getParameter("province");
		System.out.println(provice);
		
		if("sc".equals(provice)) {
			out.println("cz:崇州,sl:双流,px:郫县");
		}else {
			out.println("da:大足,hc:合川");
		}
		
	}
	
	private void random_number(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
		System.out.println("random_number()"); // 追踪
		PrintWriter out = resp.getWriter();
		
		Random r = new Random();
		Integer number = r.nextInt(100);
		System.out.println(number);
		
		out.println(number);
	}
}
